*Examines properties of already produced forecasts
import excel using "Results.xlsx", sheet("Predictions4") first clear



*Number of variables in prediction model
local k=15

gen i=_n
gen acorr=.
gen bias=.
gen biass=.
gen v=.
gen skew=.
gen kurt=.
gen mse=.
gen r2=.

*Start and end dates
local s1=19651
local e1=20224
local s2=19651
local e2=19934
local s3=19941
local e3=20224

*Initial values for bias, bias^2, variance skewness,kurtosis, mse
*For the three periods considered
local bias1=0
local bias1s=0
local v1=0
local skew1=0
local kurt1=0
local mse1=0
local r21=0

local bias2=0
local bias2s=0
local v2=0
local skew2=0
local kurt2=0
local mse2=0
local r22=0

local bias3=0
local bias3s=0
local v3=0
local skew3=0
local kurt3=0
local mse3=0
local r23=0

*Initial values for average correlations
local corr1=0
local corr2=0
local corr3=0

capture drop efxx*
capture drop sefxx*
	

foreach var of varlist fxxdp-fxxlastpredictor{


	gen e`var'=`var'-lnexret
	
	sum e`var' if yy>=`s1' & yy<=`e1', det
	local bias1=`bias1'+r(mean)/`k'
	local bias1s=`bias1s'+(r(mean))^2/`k'

	
	sum e`var' if yy>=`s1' & yy<=`e1', det
	local v1=`v1'+ r(Var)/`k'*(r(N)-1)/r(N)
	local skew1=`skew1'+r(skewness)/`k'
	local kurt1=`kurt1'+r(kurtosis)/`k'

	
	sum e`var' if yy>=`s2' & yy<=`e2', det
	local bias2=`bias2'+r(mean)/`k'
	local bias2s=`bias2s'+(r(mean))^2/`k'

	sum e`var' if yy>=`s2' & yy<=`e2', det
	local v2=`v2'+r(Var)/`k'*(r(N)-1)/r(N)
	local skew2=`skew2'+r(skewness)/`k'
	local kurt2=`kurt2'+r(kurtosis)/`k'
	
	sum e`var' if yy>=`s3' & yy<=`e3', det
	local bias3=`bias3'+r(mean)/`k'
	local bias3s=`bias3s'+(r(mean))^2/`k'

	
	sum e`var' if yy>=`s3' & yy<=`e3', det
	local v3=`v3'+r(Var)/`k'*(r(N)-1)/r(N)
	local skew3=`skew3'+r(skewness)/`k'
	local kurt3=`kurt3'+r(kurtosis)/`k'
	
}

foreach var of varlist fxxdp-fxxlastpredictor{

	gen se`var'=(`var'-lnexret)^2
	
	sum se`var' if yy>=`s1' & yy<=`e1', det
		local mse1=`mse1'+r(mean)/`k'
		
	sum se`var' if yy>=`s2' & yy<=`e2', det
		local mse2=`mse2'+r(mean)/`k'
	
	sum se`var' if yy>=`s3' & yy<=`e3', det
		local mse3=`mse3'+r(mean)/`k'
	
}

	
*Write results in variables
	
replace bias=`bias1' in 1
replace bias=`bias2' in 2
replace bias=`bias3' in 3

replace biass=`bias1s' in 1
replace biass=`bias2s' in 2
replace biass=`bias3s' in 3

replace v=`v1' in 1
replace v=`v2' in 2
replace v=`v3' in 3

replace skew=`skew1' in 1
replace skew=`skew2' in 2
replace skew=`skew3' in 3

replace kurt=`kurt1' in 1
replace kurt=`kurt2' in 2
replace kurt=`kurt3' in 3

replace mse=`mse1' in 1
replace mse=`mse2' in 2
replace mse=`mse3' in 3


*MSE for the five combination methods and the benchmark

foreach var of varlist mean-dmspe1 modelmean{

	gen se`var'=(`var'-lnexret)^2
	sum se`var' if yy>=`s1' & yy<=`e1', det
	gen mse`var'=r(mean) in 1
	
	sum se`var' if yy>=`s2' & yy<=`e2', det
	replace mse`var'=r(mean) in 2
	
	sum se`var' if yy>=`s3' & yy<=`e3', det
	replace mse`var'=r(mean) in 3
	
}

*R^2values

foreach var of varlist fxxdp-fxxlastpredictor{
	
	sum se`var' if yy>=`s1' & yy<=`e1', det
	local r21=`r21'+(1-r(mean)/msemodelmean[1])/`k'
		
	sum se`var' if yy>=`s2' & yy<=`e2', det
	local r22=`r22'+(1-r(mean)/msemodelmean[2])/`k'
	
	
	sum se`var' if yy>=`s3' & yy<=`e3', det
	local r23=`r23'+(1-r(mean)/msemodelmean[3])/`k'
	
}


replace r2=`r21' in 1
replace r2=`r22' in 2
replace r2=`r23' in 3


*Bias squared of mean combination forecast

	egen cavbias2=rowmean(fxx*)
	replace cavbias=(cavbias-lnexret)^2
	
	sum cavbias2 if yy>=`s1' & yy<=`e1', det
	gen avbias2=r(mean) in 1
	
	sum cavbias2 if yy>=`s2' & yy<=`e2', det
	replace avbias2=r(mean) in 2
	
	sum cavbias2 if yy>=`s3' & yy<=`e3', det
	replace avbias2=r(mean) in 3
	


*Determine average correlation of forecast errors
	
alpha efxx* if yy>=`s1' & yy<=`e1', std		
replace acorr=r(rho) in 1

alpha efxx* if yy>=`s2' & yy<=`e2', std		
replace acorr=r(rho) in 2
	
alpha efxx* if yy>=`s3' & yy<=`e3', std		
replace acorr=r(rho) in 3

export excel v bias biass mse msemean msemedian msetrim msedmspe1 msedmspe9 msemodelmean acorr avbias2 r2 using results.xlsx in 1/3, sheetmod sheet("Table8") cell(A1) first(var)




*Analysis with hypothetical correlations

*Vector of weights
matrix w = J(`k', 1, 1/`k')
*Initialize D and b for second half of sample period
matrix D2 = I(`k')
matrix b2 = J(`k', 1, 0)
*Initialize D for hypothetical values based on first half relative variance
matrix D1 = I(`k')


*Correlation matrix first half
cor efx* if yy>=`s2' & yy<=`e2'
matrix C1 = r(C)



*Determine R² assuming that correlations are equal to correlations from first half
local i=1
foreach var of varlist efxxdp-efxxlastpredictor{
	sum `var' if yy>=`s3' & yy<=`e3'
	matrix D2[`i',`i']=r(sd)*((r(N)-1)/r(N))^0.5
	matrix b2[`i',1]=r(mean)
	local i=`i'+1
}
matrix w = J(15, 1, 1/15)
matrix bias2=w'*b2
matrix bias2sq=bias2[1,1]^2
matrix mspe = bias2sq + w' * D2 * C1 * D2 * w

gen mspewith1sthalf=mspe[1,1] in 1

capture drop ehistsq
gen ehistsq=(lnexret-modelmean)^2
sum ehistsq if yy>=`s3' & yy<=`e3'
local mspehist=r(mean)

local r2withcorrof1sthalf=1-mspe[1,1]/`mspehist'

gen r2with1sthalf=`r2withcorrof1sthalf' in 1


*Determine R² assuming that correlations are equal to average correlation from first half

alpha efxx* if yy>=`s2' & yy<=`e2', std		
matrix Cconst = J(`k',`k',r(rho))
forvalues i=1/`k'{
	matrix Cconst[`i',`i'] = 1
}

matrix mspeconst = bias2sq + w' * D2 * Cconst * D2 * w

local r2withcorrconst=1-mspeconst[1,1]/`mspehist'
gen r2with1sthalfconst=`r2withcorrconst' in 1


*Determine fraction of correlations that increased from first half to second
cor efx* if yy>=`s3' & yy<=`e3'
matrix C2 = r(C)
local frac=0
local numcorr=`k'^2-`k'

forvalues i=1/`k'{
	forvalues j=1/`k'{
		if C2[`i',`j']>C1[`i',`j'] & `i'!=`j'{
			local frac=`frac' + 1/`numcorr'	
		}
	}
}
di `frac'
gen frachighercorr=`frac'





export excel r2with1sthalf r2with1sthalfconst frachighercorr mspewith1sthalf using results.xlsx in 1/1, sheetmod sheet("Table8") cell(A5) first(var)
